package com.yanq.liancar.activity;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/6  16:46
 *  @描述       ${TODO}
 */


import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.ShopByPrecisionBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VehicleTrendsActivity extends BaseToolbarActivity implements AMap.OnMarkerClickListener {
    @BindView(R.id.tv_whole)
    TextView tvWhole;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.tv_travel)
    TextView tvTravel;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.tv_Stop)
    TextView tvStop;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.tv_Offline)
    TextView tvOffline;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.mapview)
    MapView mapview;
    @BindView(R.id.ll_cardetails)
    LinearLayout ll_cardetails;
    @BindView(R.id.img_car)
    ImageView imgCar;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_carstatus)
    TextView tvCarstatus;
    @BindView(R.id.tv_seetime)
    TextView tvSeetime;
    private AMap mAMap;
    //声明AMapLocationClient类对象
    private AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    private AMapLocationClientOption mLocationOption = null;
    //定义一个UiSettings对象
    private UiSettings mUiSettings;

    private Marker marker;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vehicletrends);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        mapview.onCreate(savedInstanceState);
        mTvTitle.setText("车辆动态");
        tvWhole.setTextColor(getResources().getColor(R.color.main_color));
        view1.setVisibility(View.VISIBLE);
        view1.setBackgroundColor(getResources().getColor(R.color.main_color));
        init();
    }

    /**
     * 初始化AMap对象
     */

    private void init() {

        if (mAMap == null) {
            mAMap = mapview.getMap();
            mUiSettings = mAMap.getUiSettings();
            mUiSettings.setZoomControlsEnabled(true);  //显示缩放按钮
            mUiSettings.setZoomPosition(AMapOptions.LOGO_MARGIN_RIGHT);
            mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);
            mAMap.moveCamera(CameraUpdateFactory.zoomTo(12));
            mAMap.setOnMarkerClickListener(this);
        }


    }

    /**
     * 添加带生长效果marker
     */

    private void addGrowMarker(LatLng latLng) {
        MarkerOptions options = new MarkerOptions();
        options.position(latLng);
        options.visible(true);
        options.title("救援地点");
        options.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.icon_openmap_mark)));
        options.setFlat(false);
        options.anchor(0.5f, 1.0f);
        marker = mAMap.addMarker(options);
        marker.showInfoWindow();
        //        Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
        //
        //        markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
        //
        //        marker.setAnimation(markerAnimation);


    }

    @OnClick({R.id.tv_whole, R.id.tv_travel, R.id.tv_Stop, R.id.tv_Offline})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_whole:
                tvWhole.setTextColor(getResources().getColor(R.color.main_color));
                view1.setVisibility(View.VISIBLE);
                view1.setBackgroundColor(getResources().getColor(R.color.main_color));
                tvTravel.setTextColor(getResources().getColor(R.color.background_content));
                view2.setVisibility(View.GONE);
                tvStop.setTextColor(getResources().getColor(R.color.background_content));
                view3.setVisibility(View.GONE);
                tvOffline.setTextColor(getResources().getColor(R.color.background_content));
                view4.setVisibility(View.GONE);
                break;
            case R.id.tv_travel:
                tvWhole.setTextColor(getResources().getColor(R.color.background_content));
                view1.setVisibility(View.GONE);
                tvTravel.setTextColor(getResources().getColor(R.color.main_color));
                view2.setVisibility(View.VISIBLE);
                view2.setBackgroundColor(getResources().getColor(R.color.main_color));
                tvStop.setTextColor(getResources().getColor(R.color.background_content));
                view3.setVisibility(View.GONE);
                tvOffline.setTextColor(getResources().getColor(R.color.background_content));
                view4.setVisibility(View.GONE);
                break;
            case R.id.tv_Stop:
                tvWhole.setTextColor(getResources().getColor(R.color.background_content));
                view1.setVisibility(View.GONE);
                tvTravel.setTextColor(getResources().getColor(R.color.background_content));
                view2.setVisibility(View.GONE);
                tvStop.setTextColor(getResources().getColor(R.color.main_color));
                view3.setVisibility(View.VISIBLE);
                view3.setBackgroundColor(getResources().getColor(R.color.main_color));
                tvOffline.setTextColor(getResources().getColor(R.color.background_content));
                view4.setVisibility(View.GONE);
                break;
            case R.id.tv_Offline:
                tvWhole.setTextColor(getResources().getColor(R.color.background_content));
                view1.setVisibility(View.GONE);
                tvTravel.setTextColor(getResources().getColor(R.color.background_content));
                view2.setVisibility(View.GONE);
                tvStop.setTextColor(getResources().getColor(R.color.background_content));
                view3.setVisibility(View.GONE);
                tvOffline.setTextColor(getResources().getColor(R.color.main_color));
                view4.setVisibility(View.VISIBLE);
                view3.setBackgroundColor(getResources().getColor(R.color.main_color));
                break;
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        double lat = marker.getOptions().getPosition().latitude;
        double lon = marker.getOptions().getPosition().longitude;
        getShopMessage(lat, lon);
        return true;
    }

    private void getShopMessage(double lat, double lon) {
        OkGo.<String>post(Config.s + "llc/shop/queryShopByPrecision")
                .tag(this)
                .params("lat", lat)
                .params("lon", lon)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        ShopByPrecisionBean baseBean = gson.fromJson(response.body(), ShopByPrecisionBean.class);
                        if (baseBean.isIsSuccess()) {
                            ll_cardetails.setVisibility(View.VISIBLE);
                            RequestOptions options = new RequestOptions();
                            options.error(R.mipmap.item_fuwu);
//                            Glide.with(mContext).load(baseBean.getData().getShopPage()).apply(options).into(imgLogo);
//                            shopTitle.setText(baseBean.getData().getShopName());
//                            shopAddress.setText(baseBean.getData().getShopAddress());
//                            tvShopaddress.setText(new BigDecimal(baseBean.getData().getLang()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "Km");
//                            tvShoppinglun.setText(baseBean.getData().getRamarkNum() + "");
//                            text.setText(baseBean.getData().getAttenNum() + "");
                        } else {
                            RxToast.error(baseBean.getOutputMessage());
                        }
                    }
                });
    }
}
